package com.jy.adscm.dealer.controller;


import com.atomikos.icatch.imp.thread.TaskManager;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.dealer.bean.Dealer;
import com.jy.adscm.dealer.service.DealerService;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.service.JyUserService;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-01-16
 */
@RestController
@RequestMapping("/adscm/dealer/dealer")
public class DealerController{

    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    DealerService dealerService;
    @Autowired
    JyUserService jyUserService;



    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody Dealer dealer, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean insert = dealerService.insertDealer(dealer,token);
        return new SimpleCoreResponse("200","新增成功",insert);
    }

    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer id, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);

        boolean result = dealerService.deleteDealer(id ,token);
        return new  SimpleCoreResponse("200","删除成功",result);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody Dealer dealer, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean result = dealerService.updateDealer(dealer,token);
        return  new SimpleCoreResponse("200","修改成功",result);
    }

    @RequestMapping("selectAll")
    public SimpleCoreResponse selectAll( HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        List<Dealer> result = dealerService.selectAll();
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("selectDealerInfo")
    public SimpleCoreResponse selectDealerInfo( HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        List<Dealer> result = dealerService.selectDealerInfo();
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("selectById/{id}")
    public SimpleCoreResponse selectById(@PathVariable Integer id, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
          Dealer dealer = dealerService.selectById(id);
        return  new SimpleCoreResponse("200","修改成功",dealer);
    }

    /**
     * 查询经销商管理人员，根据当前登录用户所在的经销商
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectDealerManager")
    public SimpleCoreResponse selectDealerManager( HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        JyUser user = AdscmBaseUtils.getInstance().getUser(tokenId,tokenRedisTemplate);

        if (user != null){
            Integer unitId = user.getUnitId();
            if (unitId == 0){
                throw new AdscmSystemException("当前登录用户属于生产厂商，不能查询经销商的管理人员！");
            }
            Dealer dealer = dealerService.selectById(unitId);
            if (dealer != null){
                String jyManager = dealer.getJyManager();
                try {
                    Integer userId = Integer.parseInt(jyManager);
                    JyUser manager = jyUserService.selectById(userId);

                    return  new SimpleCoreResponse("200","查询成功",manager);

                }catch (Exception e){
                    throw new AdscmSystemException("经销商查询失败："+e.getMessage());
                }

            }else {
                throw new AdscmSystemException("经销商查询失败！");
            }


        }else {
            throw new AdscmSystemException("没有查询到当前登录的用户，请重新登录！");
        }


    }

}

